package com.weixin.paleituo.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.base.BaseRecycleAdapter;
import com.weixin.paleituo.bean.CouponsBean;
import com.weixin.paleituo.help.RecycleViewHolder;
import com.weixin.paleituo.util.UIUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;

/**
 * 优惠券展示、选择界面
 * 提供多选功能，选择多张优惠券
 *
 * @author fengzhen
 * @version v1.0, 2018/4/2
 */
public class CouponsSelectorRecycleAdapter<T> extends BaseRecycleAdapter<T> {

    /**
     * 保存已选项
     * K:position V:bean
     */
    private Map<Integer, Integer> mChooseCoupons = new HashMap<>();

    public CouponsSelectorRecycleAdapter(Context context, List<T> datas) {
        super(context, datas, R.layout.item_coupons_selector);
    }

    /**
     * 获取所有已选择项
     */
    public Collection<Integer> getAllSelect() {
        return mChooseCoupons.values();
    }

    /**
     * 设置默认选中的优惠券类型，根据优惠券id进行初始化选中
     *
     * @param select 已经选择的优惠券
     */
    public void setChooseCoupons(List<Integer> select) {
        // 初始化默认选中的
        if (mDatas != null && select != null) {
            for (int i = 0, j = mDatas.size(); i < j; i++) {
                CouponsBean t1 = (CouponsBean) mDatas.get(i);
                for (Integer t :
                        select) {
                    if (t == t1.getId()) {
                        mChooseCoupons.put(i, t);
                    }
                }
            }
        }
        notifyDataSetChanged();
    }

    @Override
    protected void setData(RecycleViewHolder holder, T s, final int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        final CouponsBean bean = (CouponsBean) mDatas.get(position);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mChooseCoupons.containsKey(position)) {
                    // 操作之前已经被选中
                    mChooseCoupons.remove(position);
                    viewHolder.mImgChooseCoupons.setEnabled(true);
                } else {
                    // 操作之前没有被选中
                    mChooseCoupons.put(position, bean.getId());
                    viewHolder.mImgChooseCoupons.setEnabled(false);
                }
            }
        });
        String money = bean.getMoney();
        SpannableString spannableString = new SpannableString(String.format(mContext.getString(R.string.string_pay), money));
        spannableString.setSpan(new AbsoluteSizeSpan(UIUtils.getDimens(R.dimen.sp_11)), 0, 1, SPAN_EXCLUSIVE_EXCLUSIVE);
        if (2 == bean.getType()) {
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.c_29CE7E)),
                    0, spannableString.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
        } else {
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.c_ffff313f)),
                    0, spannableString.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        viewHolder.mTvPriceItemCoupons.setText(spannableString);
        if (2 == bean.getType()) {
            viewHolder.mTvFromItemCoupons.setText(bean.getSysname());
            viewHolder.mTvFromItemCoupons.setTextColor(ContextCompat.getColor(mContext, R.color.c_29CE7E));
        } else {
            viewHolder.mTvFromItemCoupons.setText(bean.getCompany());
            viewHolder.mTvFromItemCoupons.setTextColor(ContextCompat.getColor(mContext, R.color.c_ffff313f));
        }
        viewHolder.mTvTypeCoupons.setText(bean.getName());
        try {
//            String[] start = bean.getStartTime().split(" ");
//            String[] end = bean.getEndTime().split(" ");
//            viewHolder.mTvUseDateCoupons.setText(start[0] + "-" + end[0]);
            if (TextUtils.isEmpty(bean.getEndTimeStr()) || "null".equals(bean.getEndTimeStr())) {
                viewHolder.mTvUseDateCoupons.setText("永久");
            } else {
                viewHolder.mTvUseDateCoupons.setText(bean.getStartTimeStr() + "-" + bean.getEndTimeStr());
            }
        } catch (Exception e) {
            e.printStackTrace();
            viewHolder.mTvUseDateCoupons.setText("永久");
        }
        // 默认选中效果
        if (mChooseCoupons.containsKey(position)) {
            viewHolder.mImgChooseCoupons.setEnabled(false);
        } else {
            viewHolder.mImgChooseCoupons.setEnabled(true);
        }
    }

    @Override
    public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(UIUtils.inflate(mContext, R.layout.item_coupons_selector, parent, false));
    }

    static class ViewHolder extends RecycleViewHolder {
        @BindView(R.id.tv_price_item_coupons)
        TextView mTvPriceItemCoupons;
        @BindView(R.id.tv_from_item_coupons)
        TextView mTvFromItemCoupons;
        @BindView(R.id.tv_type_coupons)
        TextView mTvTypeCoupons;
        @BindView(R.id.tv_use_date_coupons)
        TextView mTvUseDateCoupons;
        @BindView(R.id.img_choose_coupons)
        ImageView mImgChooseCoupons;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
